package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("userAddressController")
@RequestMapping("/user/addressBook")
public class AddressController {
    @Autowired
    private AddressService addressService;

    @PostMapping
    public Result add(@RequestBody AddressBook addressBook){
        addressService.add(addressBook);
        return Result.success();
    }
    @GetMapping("/list")
    public Result<List<AddressBook>> selectAll(){
        addressService.selectAll();
        return Result.success(addressService.selectAll());
    }

    @GetMapping("/default")
    public Result<AddressBook> getDefault(){
        AddressBook addressBook1= addressService.getDefault();
        if (addressBook1==null) {
            return Result.error("没有默认地址");
        }
        return Result.success(addressService.getDefault());
    }
    @GetMapping("/{id}")
    public Result<AddressBook> getById(Long id){
        return Result.success(addressService.getById(id));
    }
    @PatchMapping
    public Result delectById(Long id){
        addressService.deleteById(id);
        return Result.success();
    }

    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefault(@RequestBody AddressBook addressBook) {
        addressService.setDefault(addressBook);
        return Result.success();
    }



}
